package com.bootdo.wx.controller;

import com.bootdo.wx.domain.GoodsBrandDO;
import com.bootdo.wx.domain.GoodsSpecDO;
import com.bootdo.wx.domain.GoodsSpecValuesDO;
import com.bootdo.wx.domain.GoodsTypeDO;
import com.bootdo.wx.service.*;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 商品类型
 * 
 * @author changwj
 * @email changwj0120@163.com
 * @date 2018-07-21 14:47:33
 */
 
@Controller
@RequestMapping("/wx/goodsType")
public class GoodsTypeController {
	@Autowired
	private GoodsTypeService goodsTypeService;
	@Autowired
	private GoodsBrandService goodsBrandService;
	@Autowired
	private GoodsSpecService goodsSpecService;
	@Autowired
	private GoodsSpecValuesService goodsSpecValuesService;
	@Autowired
	private GoodsTypeSpecService goodsTypeSpecService;
	@Autowired
	private GoodsTypePropsService goodsTypePropsService;
	@Autowired
	private GoodsTypeParamgService goodsTypeParamgService;
	@Autowired
	private GoodsTypeBrandService goodsTypeBrandService;
	
	@GetMapping()
	@RequiresPermissions("wx:goodsType:goodsType")
	String GoodsType(){
	    return "wx/goodsType/goodsType";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("wx:goodsType:goodsType")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<GoodsTypeDO> goodsTypeList = goodsTypeService.list(query);
		int total = goodsTypeService.count(query);
		PageUtils pageUtils = new PageUtils(goodsTypeList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	@RequiresPermissions("wx:goodsType:add")
	String add(Model model){
		List<GoodsBrandDO> goodsBrandDOList = goodsBrandService.queryAll();
		model.addAttribute("brandList", goodsBrandDOList);
	    return "wx/goodsType/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("wx:goodsType:edit")
	String edit(@PathVariable("id") String id,Model model){
		GoodsTypeDO goodsType = goodsTypeService.get(id);
		goodsType.setBrandList(goodsTypeBrandService.queryAndBrandByTypeId(id));
		//下方可用
//		goodsType.setSpecList(goodsTypeSpecService.queryListAndValueByTypeId(id));
//		goodsType.setParamgList(goodsTypeParamgService.queryListAndValueByTypeId(id));
		model.addAttribute("goodsType", goodsType);
		//model.addAttribute("brandList", goodsBrandService.queryAll());
		model.addAttribute("specList", goodsTypeSpecService.queryListAndValueByTypeId(id));
		//弃用
//		model.addAttribute("propsList", goodsTypePropsService.queryListAndValueByTypeId(id));
		model.addAttribute("paramgList", goodsTypeParamgService.queryListAndValueByTypeId(id));
	    return "wx/goodsType/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("wx:goodsType:add")
	public R save(GoodsTypeDO goodsType,@RequestParam(value="brand",required = false) String[] brand, @RequestParam(value = "specId",required = false) String[] specId,
                   @RequestParam(value = "specValIds",required = false) String[] specValIds, @RequestParam(value = "specValText",required = false) String[] specValText, @RequestParam(value = "specValUrl",required= false) String[] specValUrl,
                   @RequestParam(value = "group_name",required = false) String[] group_name, @RequestParam(value = "group_params",required = false) String[] group_params){
		
		if(goodsType==null || goodsType.getName()==null ||"".equals(goodsType.getName())){
			return R.error("类型名字不能为空");
		}
	
		
		if(specId== null || specId.length<=0){
			return R.error("必须添加一个规格");
		}
		if(specValIds ==null || specValIds.length<=0){
			return R.error("最少选一个规格值");
		}
	/*	if(group_name== null ||group_name.length<=0){
			return R.error("最少填一个详细参数");
		}
		if(group_params== null ||group_params.length<=0){
			return R.error("最少填一个详细参数值");
		}*/
		if(goodsTypeService.add(goodsType, brand, specId, specValIds, specValText, specValUrl, group_name, group_params)>0){
			return R.ok();
		}
		
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("wx:goodsType:edit")
	public R update( GoodsTypeDO goodsType, @RequestParam(value = "brand",required=false) String[] brand, @RequestParam(value = "specId",required=false) String[] specId,
					 @RequestParam(value = "specValIds",required=false) String[] specValIds, @RequestParam(value = "specValText",required=false) String[] specValText, @RequestParam(value = "specValUrl",required=false) String[] specValUrl,
					 @RequestParam(value="group_name",required=false) String[] group_name, @RequestParam(value="group_params",required=false) String[] group_params){
		if(goodsType==null || goodsType.getName()==null ||"".equals(goodsType.getName())){
			return R.error("类型名字不能为空");
		}
		
		goodsTypeService.edit(goodsType,brand, specId, specValIds, specValText, specValUrl, group_name, group_params);
//		goodsTypeService.update(goodsType);
		
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("wx:goodsType:remove")
	public R remove( String id){
		GoodsTypeDO goodsTypeDO = goodsTypeService.get(id);
		goodsTypeDO.setDelFlag(1);
		goodsTypeDO.setGmtModified(new Date());
		if(goodsTypeService.update(goodsTypeDO)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("wx:goodsType:batchRemove")
	public R remove(@RequestParam("ids[]") String[] ids){
		for (String id : ids) {
			GoodsTypeDO goodsTypeDO = goodsTypeService.get(id);
			goodsTypeDO.setDelFlag(1);
			goodsTypeDO.setGmtModified(new Date());
			goodsTypeService.update(goodsTypeDO);
		}
		return R.ok();
	}

	/**
	 * 查询所有规格
	 */

	@GetMapping("/spec")
	String spec(Model model){
		List<GoodsSpecDO> list = goodsSpecService.queryAll();
		model.addAttribute("goodsSpec",list);
		return "wx/goodsType/spec";
	}

	/**
	 * 根据ID查询规格的值
	 */
	@ResponseBody
	@PostMapping("/specVal")
	public R spec_val(@RequestParam("id") String id){
		List<GoodsSpecValuesDO> list = goodsSpecValuesService.query(id);
		return R.ok().put("data",list);
	}

}
